package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.Reward1;
import com.woniu.entity.Statement1;
import com.woniu.pojo.Class;
import com.woniu.pojo.Reward;
import com.woniu.pojo.Statement;
import com.woniu.pojo.Student;
import com.woniu.service.ClassService;
import com.woniu.service.RewardService;
import com.woniu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller

@RequestMapping("reward")
public class RewardController {
    @Autowired
    RewardService rewardService;
    @Autowired
    StudentService studentService;
    @Autowired
    ClassService classService;

    @RequestMapping("listJump")
    public String listJump(Model model){
        List<Student> studentList = studentService.selectAll();
        List<Class> classList = classService.selectAll();
        System.out.println("---------------------------------");
        System.out.println(studentList.size());
        System.out.println(classList.size());
        System.out.println("---------------------------------");
        model.addAttribute("studentList",studentList);
        model.addAttribute("classList",classList);
        return "WEB-INF/reward/list";
    }
    //增加页面跳转
    @RequestMapping("addJump")
    public String addJump(Model model){
        List<Student> studentList = studentService.selectAll();
        List<Class> classList = classService.selectAll();
        model.addAttribute("studentList",studentList);
        model.addAttribute("classList",classList);
        return "WEB-INF/reward/add";
    }
    //增加奖惩数据
    @RequestMapping("add")
    @ResponseBody
    public Map<String,Object> add(Reward reward){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            rewardService.add(reward);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    //按住键删除奖惩数据
    @RequestMapping("deleteById")
    @ResponseBody
    public Map<String,Object> deleteById(Integer rewardId){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            rewardService.deleteById(rewardId);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    //查到一个奖惩数据
    @RequestMapping("findOne")
    @ResponseBody
    public Map<String, Object> findOne(Integer rewardId){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            Reward reward =rewardService.findOne(rewardId);
            map.put("reward",reward);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    //跳转到修改
    @RequestMapping("updateJump")
    public String updateJump(Model model, Integer rewardId){
        List<Student> studentList = studentService.selectAll();
        List<Class> classList = classService.selectAll();
        model.addAttribute("studentList",studentList);
        model.addAttribute("classList",classList);
        Reward reward =rewardService.findOne(rewardId);
        model.addAttribute("reward",reward);
        return "WEB-INF/reward/update";
    }
    //修改奖惩数据
    @RequestMapping("updateById")
    @ResponseBody
    public Map<String, Object> updateById(Reward reward){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            System.out.println("#################################################");
            System.out.println(reward);
            System.out.println("#################################################");
            rewardService.updateById(reward);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    //奖惩数据查询分页
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Reward1> rewardList=rewardService.findAll1();

            PageInfo<Reward1> pageInfo=new PageInfo<Reward1>(rewardList);
            for (Reward1 r:rewardList
                 ) {
                System.out.println(r);
            }
            System.out.println(rewardList.get(0));
            map.put("pageInfo",pageInfo);
            map.put("rewardList",rewardList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }

    //奖惩数据批量删除
    @RequestMapping("deleteByExample")
    @ResponseBody
    public Map<String,Object> deleteByExample(Integer[] rewardIds){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            rewardService.deleteByExample(Arrays.asList(rewardIds));
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    @RequestMapping("findDate")
    @ResponseBody
    public Map<String, Object> findDate( Reward reward, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize){
        Map<String, Object> map=new HashMap<String,Object>();
        Map<String, Object> mapCondition=new HashMap<String,Object>();
        mapCondition.put("reward",reward);
        System.out.println("ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt");
        System.out.println(reward);
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Reward1>  rewardList=rewardService.findDate(mapCondition);
            PageInfo<Reward1> pageInfo=new PageInfo<Reward1>(rewardList);
            map.put("pageInfo",pageInfo);
            map.put("rewardList",rewardList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }

}
